<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\model as M;

/**
 * 提醒生日
 */
class Noticebirthday extends Command
{
    protected function configure()
    {
        $this->setName('noticebirthday')
            ->setDescription('提醒生日');
    }

    /**
     * 每天早上7点执行一次即可
     * 提前一天推给所属销售
     * 当天推送给客户
     * @Author   FW
     * @DateTime 2024-07-10
     * @Email    123763712@qq.com
     * @param    Input            $input  [description]
     * @param    Output           $output [description]
     * @return   [type]                   [description]
     */
    protected function execute(Input $input, Output $output)
    {
        $salesNums = $customerNums = 0;

        ## 提前一天推给所属销售
        $birthday = date('m-d',strtotime('+1 days'));
        $users = M\User::field('uid,avatar,mobile,nickname,full_name,openid,birthday,spread_suid,sales_admin_id')
        ->where('birthday','LIKE', "%$birthday%")
        ->select();

        foreach ( $users as $user ) {
            if ( !$user['spread_suid'] && !$user['sales_admin_id'] ) continue;
            $res = $this->generateMessageTaskForSales($user);

            if ( $res !== false ) $salesNums++;
        }

        ## 当天推送给客户
        $birthday = date('m-d');
        $users = M\User::field('uid,avatar,mobile,nickname,full_name,openid,birthday,spread_suid,sales_admin_id')
        ->where('birthday','LIKE', "%$birthday%")
        ->select();

        foreach ( $users as $user ) {
            $res = $this->generateMessageTaskForCustomers($user);

            if ( $res !== false ) $customerNums++;
        }

        return $output->writeln(sprintf('发送隔天销售提醒： %d 条，发送当天客户生日提醒： %d 条', $salesNums, $customerNums));
    }

    /**
     * 给销售发送其名下明日将要过生日的客户提醒
     * @Author   FW
     * @DateTime 2024-08-06
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    private function generateMessageTaskForSales($user)
    {
        $summary = sprintf("您有客户将于明天（%s）过生日，请及时为TA送上祝福吧。" , date('m-d',strtotime('+1 days')));
        $content = sprintf('
            <div>
                <div class="flex">
                    <div style="width: 120rpx;">客户UID</div>
                    <div>%d</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">客户姓名</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">客户昵称</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">手机号码</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">客户生日</div>
                    <div>%s</div>
                </div>
            </div>
            <div style="font-size: 22rpx; color: #999; margin-top:20rpx">备注：系统会在客户生日当天自动发送贺卡消息</div>
        ' , $user['uid'] , ($user['full_name'] ?? '未实名'), $user['nickname'] , $user['mobile'] , $user['birthday']);

        $uid = $user['spread_suid'];
        if ( !$uid && !empty($user['sales_admin_id']) ) {
            $uid = M\TeacherInfo::where('admin_id',$user['sales_admin_id'])->value('uid');
        }

        ## 检查一下当天是否已经发送过了
        $todaySends   = M\Message::where([
            'mtype'      => M\Message::MSG_TYPE_BIRTHDAYB,
            'uid'        => $uid,
        ])->where('send_time' , 'BETWEEN' , [date('Y-m-d 00:00:00') , date('Y-m-d 23:59:59')])->count();
        if ( $todaySends > 0 ) return false;

        return M\Message::saveOne([
            'title'         => '客户生日提醒',
            'summary'       => $summary,
            'content'       => htmlspecialchars($content),
            'mtype'         => M\Message::MSG_TYPE_BIRTHDAYB,
            'mgap_days'     => 1,
            'uid'           => $uid,
            'cycle'         => 1,
            'send_time'     => date('Y-m-d H:i:s'),
        ]);
    }

    /**
     * 给今天将要过生日的客户发送贺卡消息
     * @Author   FW
     * @DateTime 2024-08-06
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    private function generateMessageTaskForCustomers($user)
    {
        ## 检查一下当天是否已经发送过了
        $todaySends   = M\Message::where([
            'mtype'      => M\Message::MSG_TYPE_BIRTHDAYC,
            'uid'        => $user['uid'],
        ])->where('send_time' , 'BETWEEN' , [date('Y-m-d 00:00:00') , date('Y-m-d 23:59:59')])->count();
        if ( $todaySends > 0 ) return false;

        $summary = "祝您生日快乐！";
        $content = $user['full_name'] ?? $user['nickname'];

        return M\Message::saveOne([
            'title'         => '祝您生日快乐',
            'summary'       => $summary,
            'content'       => $content,
            'mtype'         => M\Message::MSG_TYPE_BIRTHDAYC,
            'mgap_days'     => 1,
            'uid'           => $user['uid'],
            'cycle'         => 1,
            'send_time'     => date('Y-m-d H:i:s'),
        ]);
    }
}// Class End